package com.orangelife.service.propFeeDic.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeDic.IPropFeeDicService;
import com.orangelife.service.propFeeDic.model.PropFeeDicDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeDic类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-07-04 11:04:40
 * ==============================================================
 */
@Service("propFeeDicService")
public class PropFeeDicServiceImpl extends MybaitsGenericDaoSubLogic implements
		IPropFeeDicService {
	private static Logger logger = Logger
			.getLogger(PropFeeDicServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeDicServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeDic(PropFeeDicDto propFeeDic) throws AppBizException {

		Assert.notNull(propFeeDic);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeDic.getCode())) {
			if (propFeeDic.getCode().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "编码长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeDic.getName())) {
			if (propFeeDic.getName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeDic.getVal())) {
			if (propFeeDic.getVal().length() > 2048) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "值长度超出");
			}
		}

		propFeeDic.setDicID(IdUtil.getuuId());
		propFeeDic.setItemStat(TypeConst.FLAG_ON);
		propFeeDic.setTscreate(new Date());
		propFeeDic.setTsedit(new Date());
		return this.getSqlSession().insert("PropFeeDicMapper.insertSelective",
				propFeeDic);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeDic(PropFeeDicDto propFeeDic) throws AppBizException {

		Assert.notNull(propFeeDic);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeDic.getDicID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeDicDto oldPropFeeDic = this.findPropFeeDicByID(propFeeDic
				.getDicID());

		if (!StringUtils.isBlank(propFeeDic.getCode())) {
			if (propFeeDic.getCode().length() > 32) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "编码长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeDic.getName())) {
			if (propFeeDic.getName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeDic.getVal())) {
			if (propFeeDic.getVal().length() > 2048) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "值长度超出");
			}
		}

		if (oldPropFeeDic == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeDic.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeDicMapper.updateByPrimaryKeySelective", propFeeDic);
	}

	@Override
	public PropFeeDicDto findPropFeeDicByID(String propFeeDicID)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeDicID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeDicDto oldPropFeeDic = this.getSqlSession().selectOne(
				"PropFeeDicSearchMapper.selectByPrimaryKey", propFeeDicID);
		if (oldPropFeeDic == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeDic.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeDic;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeDicByLogic(String propFeeDicID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeDicID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeDicDto oldPropFeeDic = this.findPropFeeDicByID(propFeeDicID);

		oldPropFeeDic.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeDic.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeDicMapper.updateByPrimaryKeySelective", oldPropFeeDic);
	}

	@Override
	public List<Map<String, Object>> findPropFeeDicByPage(String commID,
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("commID", commID);
		map.put("search", search);

		/*
		 * if (!StringUtils.isBlank(search)) { Map<String, Object> pam = new
		 * HashMap<String, Object>();
		 * 
		 * pam.put("search", search); List<Map<String, Object>> comms =
		 * JsonMapUtils
		 * .getPageEntityMap(HttpSvcUtils.doGetSvc(UrlPath.searComm, pam,
		 * null)); List<String> commIDs = new ArrayList<String>();
		 * commIDs.add("-1"); for (Map<String, Object> comm : comms) {
		 * commIDs.add(comm.get("commID").toString());
		 * 
		 * } map.put("commIDs", commIDs); map.put("search", search);
		 * 
		 * }
		 */
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeDicSearchMapper.findAllPropFeeDic", map);
		/*
		 * if (list != null && list.size() > 0) {
		 * 
		 * List<String> commIDs = new ArrayList<String>(); for (Map<String,
		 * Object> open : list) { if (open.get("comm_id") != null) {
		 * commIDs.add(open.get("comm_id").toString()); } } if (commIDs != null
		 * && commIDs.size() > 0) { List<Map<String, Object>> comms =
		 * JsonMapUtils .getPageEntityMap(HttpSvcUtils.doPostSvc(
		 * UrlPath.bauthComm, commIDs, null)); Map<String, String> connValue =
		 * new HashMap<String, String>(); connValue.put("comm", "comm"); list =
		 * SplicingUtils.connoList(list, comms, "comm_id", "commID", connValue);
		 * } }
		 */
		return list;
	}

	@Override
	public List<PropFeeDicDto> getPropFeeDicByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeDicDto> list = this.getSqlSession().selectList(
				"PropFeeDicSearchMapper.getAllPropFeeDicDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeDicByRelevantID(String dicID,
			String commID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dicID", dicID);
		map.put("commID", commID);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeDicSearchMapper.selectPropFeeDicDtoyRelevantID", map);
		return list;
	}

	@Override
	public List<PropFeeDicDto> findPropFeeDicByRelevantID(String dicID,
			String commID, String code) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dicID", dicID);
		map.put("commID", commID);
		map.put("code", code);

		List<PropFeeDicDto> list = this.getSqlSession().selectList(
				"PropFeeDicSearchMapper.findPropFeeDicDtoByRelevantID", map);
		return list;
	}

	@Override
	public List<PropFeeDicDto> bauthfindPropFeeDic(List<String> propFeeDicIDs)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeDicDto> list = this.getSqlSession().selectList(
				"PropFeeDicSearchMapper.batchFindPropFeeDic", propFeeDicIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeDicByMap(
			Map<String, Object> propFeeDic) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeDicSearchMapper.batchFindPropFeeDicByMap", propFeeDic);
		return list;
	}

	@Override
	public List<Map<String, Object>> batchFindRuleFeeDic(
			Map<String, Object> propFeeDic) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeDicSearchMapper.batchFindRuleFeeDic", propFeeDic);
		return list;
	}

}